package org.elec.mac.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Bean on 2018/2/23.
 */
@Slf4j
public class UpFileUtils {


    /**
     * 文件上传保存
     * @param request
     * @param filepath
     * @param savefile
     * @return
     */
    public static String upLoadImageFile(HttpServletRequest request, String filepath, String savefile) {
        try {
            MultipartFile multipartFile = ((MultipartHttpServletRequest) request).getFile(savefile);
            if (multipartFile != null && multipartFile.getSize() > 0) {
                log.info(multipartFile.getOriginalFilename());
                String filetype = multipartFile.getOriginalFilename().substring(multipartFile.getOriginalFilename().lastIndexOf("."));
                String filename = DateUtil.formatDate(new Date(), DateUtil.DATE_TIME_PATTERN_1) + RandomStringUtils.randomNumeric(8) + filetype;
                filepath += filename;
                FileUtils.writeByteArrayToFile(new File(filepath), multipartFile.getBytes());
                return filename;
            }
        }
        catch (Exception ex) {
            log.error("文件上传错误", ex);
        }
        return null;
    }

    /**
     * 保存文本
     * @param request
     * @param filepath
     * @param savefile
     * @return
     */
    public static List<String> upLoadTxtFile(HttpServletRequest request, String filepath, String savefile) {
        FileInputStream fileInputStream = null;
        BufferedReader reader = null;
        boolean bool = false;
        List<String> list = new ArrayList<>();
        try {
            MultipartFile multipartFile = ((MultipartHttpServletRequest) request).getFile(savefile);
            if (multipartFile != null && multipartFile.getSize() > 0) {
                log.info(multipartFile.getOriginalFilename());
                String filetype = multipartFile.getOriginalFilename().substring(multipartFile.getOriginalFilename().lastIndexOf("."));
                if (filetype.equalsIgnoreCase("txt")) {
                    return null;
                }
                String filename = DateUtil.formatDate(new Date(), DateUtil.DATE_TIME_PATTERN_1) + RandomStringUtils.randomNumeric(8) + filetype;
                filepath += filename;
                FileUtils.writeByteArrayToFile(new File(filepath), multipartFile.getBytes());
                //
                fileInputStream = (FileInputStream) multipartFile.getInputStream();
                reader = new BufferedReader(new InputStreamReader(fileInputStream));
                String line = "";
                while((line = reader.readLine()) != null) {
                    list.add(StringUtils.trim(line));
                }
            }
        }
        catch (Exception ex) {
            log.error("文件上传错误", ex);
            list = null;
        }
        finally {
            try {
                if (fileInputStream != null) {
                    fileInputStream.close();
                }
                if (reader != null) {
                    reader.close();
                }
            }
            catch (IOException oex) {
                log.error("字节流关闭异常", oex);
            }
        }
        return list;
    }
}
